<template>
	<view>
		<!-- 已设置工序表格 -->
		<zy-table ref="zyTable" :headList="headList" :dataList="dataList" :operationList="operationList" border loading>
		</zy-table>
	</view>
</template>
<script>
	import {
		queryListProcedure,
		delEntityById
	} from "@/api/index.js"
	export default {
		data() {
			return {
				//表格表头数据
				headList: [{
						name: this.$t('my.procedure.procedureCode'),
						key: 'procedureCode',
						width: 150
					},
					{
						name: this.$t('public.procedureName'),
						key: 'procedureName',
						width: 150
					},

				],
				dataList: [], //表格数据
				//表格操作列数据
				operationList: [{
					kind: "switch",
					handle: this.switchChange,
				}, {
					kind: "button",
					type: "warn",
					handle: this.delBtn,
					label: this.$t('public.del'),
				}],
				arrData: [], //获取储存的值
				arrId: [], //遍历储存的值id
				dataList1: [],
				dataList2: [],
				procedureData: [], //最新表格开关被操作的表格数据
			}
		},
		mounted() {
			this.getData()
		},
		methods: {
			// 获取表格数据
			getData() {
				this.dataList = []
				this.dataList1 = []
				this.dataList2 = []
				queryListProcedure({
					foreignId: this.$store.state.loginData.id, //人员id
				}).then((res) => {
					if (res.code == 0) {
						this.arrData = uni.getStorageSync("procedureData")
						if (this.arrData == undefined || this.arrData == '') {
							this.dataList = res.data.map(m => {
								return Object.assign(m, {
									switch: false
								})
							})
						} else {
							for (var j = 0; j < this.arrData.length; j++) {
								this.arrId.push(this.arrData[j].id)
							}
							res.data.filter((item) => {
								if (this.arrId.indexOf(item.id) !== -1) {
									this.dataList1 = this.arrData;
								}
							});
							res.data.filter((item) => {
								if (this.arrId.indexOf(item.id) == -1) {
									this.dataList2.push(item)
								}
							});
							this.dataList = this.dataList1.concat(this.dataList2)
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//表格开关
			switchChange(item, index, $event) {
				this.procedureData = []
				for (var i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].id == item.id) {
						this.dataList[i].switch = $event.detail.value
					} else {
						//控制只允许打开一个
						this.dataList[i].switch = false
					}
				}
				this.procedureData = this.dataList
				uni.setStorageSync("procedureData", this.procedureData)
			},
			// 表格删除
			delBtn(item, index) {
				delEntityById({
					ids: item.id
				}).then((res) => {
					if (res.code == 0) {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
						this.dataList.splice(index, 1)
						for (var i = 0; i < this.dataList.length; i++) {
							if (this.dataList[i].switch == undefined) {
								this.$set(this.dataList[i], 'switch', false)
							}
						}
						uni.setStorageSync("procedureData", this.dataList)
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style>
</style>